package com.huawei.hmsdemo10086.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.huawei.hmsdemo10086.POJO.Car;
import com.huawei.hmsdemo10086.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class PayAdapter extends BaseAdapter {

    private List<Car> datas = new ArrayList<Car>();
    private Context context;
    private LayoutInflater layoutInflater;
    private List<Car> choosed_data = new ArrayList<Car>();

    public List<Car> getChoosed_data() {
        return choosed_data;
    }

    public void clear_data()
    {
        choosed_data.clear();
    }

    public PayAdapter(Context context, List<Car> datas) {
        this.datas = datas;
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return datas.size(); //返回列表的长度
    }

    @Override
    public Car getItem(int position) {
        return datas.get(position); //通过列表的位置 获得集合中的对象
    }

    @Override
    public long getItemId(int position) { // 获得集合的Item的位置
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.pay_goods_layout, null);//找到布局文件
            convertView.setTag(new ViewHolder(convertView));
        }
        initViews(getItem(position), (ViewHolder) convertView.getTag());
        return convertView;

    }

    private void initViews(Car data, ViewHolder holder) {//初始化数据

        /**
         * 第一次初始话的时候通过 要请求的Url地址 为每个图片设置一个Tag标记,
         * 然后在设置图片的时候判断Tag标记如果是才把图片设置到ImageView上,
         * 这做的原因是为了防止ListView 中的图片错位...
         */

        holder.num.setText(""+data.getNum());//设置Tag

        //设置新闻标题为集合中获得的标题
        holder.title.setText(data.getGoods().getName());

        holder.price.setText(""+data.getGoods().getPrice() * data.getNum());

        //Glide设置图片圆角角度
        RoundedCorners roundedCorners = new RoundedCorners(10);
        //通过RequestOptions扩展功能,override:采样率,因为ImageView就这么大,可以压缩图片,降低内存消耗
        // RequestOptions options = RequestOptions.bitmapTransform(roundedCorners).override(20, 20);
        RequestOptions options = RequestOptions.bitmapTransform(roundedCorners);

        Glide.with(context).load(data.getGoods().getPicaddress())
                .apply(options)
                .into(holder.img);



    }

    protected class ViewHolder {
        private TextView price;
        private TextView title;
        private TextView num;
        private ImageView img;



        public ViewHolder(View view) {
            title = (TextView) view.findViewById(R.id.goods_title);
            num = (TextView) view.findViewById(R.id.goods_num);
            price = (TextView) view.findViewById(R.id.goods_price);
            img = view.findViewById(R.id.car_img);

        }
    }

}

